package com.glink.common.vo;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2024/6/14 13:52
 */

import com.glink.common.contants.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(description = "RESTFUL服务响应封装")
public class ResponseResult<T> {
    @ApiModelProperty(value = "响应状态码", example = "000000:成功,000500:异常，其它http状态码")
    private String status;
    @ApiModelProperty(value = "响应数据", example = "List<Entity>/Entity")
    private T result;
    @ApiModelProperty(value = "异常消息", example = "服务调用的异常消息")
    private String message;

    public ResponseResult() {
    }
    
    public ResponseResult(String status, T data, String message) {
        setStatus(status);
        setData(data);
        setMessage(message);
    }

    public ResponseResult(String status, String message) {
        setStatus(status);
        setMessage(message);
    }

    public static <T> ResponseResult<T> successResult() {
        return new ResponseResult<>(ErrorCode.REQ_SUCCESS.getValue(), null, null);
    }

    public static <T> ResponseResult<T> successResult(String message) {
        return new ResponseResult<>(ErrorCode.REQ_SUCCESS.getValue(), null, message);
    }

    public static <T> ResponseResult<T> successResult(T object, String message) {
        return new ResponseResult<>(ErrorCode.REQ_SUCCESS.getValue(), object, message);
    }

    public static <T> ResponseResult<T> successResult(T object) {
        return new ResponseResult<>(ErrorCode.REQ_SUCCESS.getValue(), object, "操作成功");
    }

    public static <T> ResponseResult<T> failResult(String message) {
        return new ResponseResult<>(ErrorCode.BUSINESS_EXCEPTION.getValue(), null, message);
    }

    public static <T> ResponseResult<T> failResult(String status, String message) {
        return new ResponseResult<>(status, null, message);
    }

    public String toString() {
        return "ResponseResult(status=" + getStatus() + ", result=" + this.result + ", message=" + getMessage() + ")";
    }

    public String getStatus() {
        return this.status;
    }

    public ResponseResult<T> setStatus(String status) {
        this.status = status;
        return this;
    }

    public T getData() {
        return this.result;
    }

    public ResponseResult<T> setData(T data) {
        this.result = data;
        return this;
    }

    public String getMessage() {
        return this.message;
    }

    public ResponseResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public boolean isSuccessful() {
        return getStatus().equals(ErrorCode.REQ_SUCCESS.getValue());
    }

    public ResponseResult<T> setSuccessful(boolean isSuccessful) {
        setStatus(isSuccessful ? ErrorCode.REQ_SUCCESS.getValue() : ErrorCode.BUSINESS_EXCEPTION.getValue());
        return this;
    }
}
